/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package onpu_dms_xls.kernel.common.inputParameters;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.ArrayList;
import onpu_dms_xls.exceptions.MyException;
import onpu_dms_xls.exceptions.MyXMLException;
import onpu_dms_xls.kernel.common.XML_Parsers.SAX_XML_FromDB_InputParam_Parser;
import onpu_dms_xls.kernel.common.logAndDebug.Debug;
import onpu_dms_xls.kernel.common.logAndDebug.LoggingFacade;
import org.apache.log4j.Logger;

/**
 *
 * @author snbm
 */
public class FromDB_InputParameters extends InputParameters{
    
    private int constantParam_documentClassId;
    private String constantParam_outputFolder;
    private String constantParam_outputFileName;
    private String constantParam_temporaryFolder;
    private String constantParam_singleFromDBQuery;
    private ArrayList<Variable> otherVars;

    public FromDB_InputParameters (String xml, String tempFolder) throws IOException, MyException{
        this.otherVars = new ArrayList<Variable>();
        LoggingFacade.trace.debug("started constructor of FromDB_InputParameters");
        LoggingFacade.trace.debug("creating temp file and writing to it xml");
        File temp = new File (tempFolder+"/"+"dms_fromDB_Input_temp.xml");
        if (temp.canRead()){
            temp.delete();
        }
        //xml 2 file
        LoggingFacade.trace.debug("(charset)encoding before writing xml to file = "+Charset.defaultCharset());
        FileOutputStream fos = new FileOutputStream(temp);
        fos.write(xml.getBytes("utf-8"));
        fos.close();
        LoggingFacade.trace.debug("xml written to file: "+temp);
        //parseFile
        SAX_XML_FromDB_InputParam_Parser sipp = new SAX_XML_FromDB_InputParam_Parser();
        try{
            LoggingFacade.trace.debug("starting parser...");
            sipp.runExample(temp.getAbsolutePath(), this);
        } catch (IOException e){
            throw new MyXMLException(
                    "FromDB_InputParameters",
                    "FromDB_InputParameters",
                    "sipp.runExample(temp.getAbsolutePath(), this);",
                    e.getMessage()) ;
        }

        //System.out.println(this);

        //delete file
        if (temp.delete()){
            return;
        } else {
            throw new MyXMLException("FromDB_InputParameters", "FromDB_InputParameters", "error while deleting temp file", temp.getAbsolutePath());
        }
    }

    @Override
    public String toString (){
        StringBuffer sb = new StringBuffer();
        sb.append("toString of FromDB_InputParameters:");
        sb.append("\ndocumentClassId: "+constantParam_documentClassId);
        sb.append("\noutputFolder: "+constantParam_outputFolder);
        sb.append("\noutputFileName: "+constantParam_outputFileName);
        sb.append("\ntemporaryFolder: "+constantParam_temporaryFolder);
    try {
        sb.append("\nsingleFromDBQuery: "+constantParam_singleFromDBQuery);
    } catch (NullPointerException e){
        sb.append("\nsingleFromDBQuery: "+"not inputed!");
    }

    if (otherVars != null){
        if (!otherVars.isEmpty()){
            sb.append("\nother variables:");
            for (Variable v : otherVars){
                sb.append("\n"+v.getName()+": "+v.getValue());
            }
        }
    }

    return sb.toString();
    }


    /**
     * @return the constantParam_outputFolder
     */
    public String getConstantParam_outputFolder() {
        return constantParam_outputFolder;
    }

    /**
     * @param constantParam_outputFolder the constantParam_outputFolder to set
     */
    public void setConstantParam_outputFolder(String constantParam_outputFolder) {
        this.constantParam_outputFolder = constantParam_outputFolder;
    }

    /**
     * @return the constantParam_outputFileNameTemplate
     */
    public String getConstantParam_outputFileName() {
        return constantParam_outputFileName;
    }

    /**
     * @param constantParam_outputFileName the constantParam_outputFileNameTemplate to set
     */
    public void setConstantParam_outputFileName(String constantParam_outputFileName) {
        this.constantParam_outputFileName = constantParam_outputFileName;
    }

    /**
     * @return the zeroVars
     */
    public ArrayList<Variable> getOtherVars() {

        return otherVars;
    }

    /**
     * @param otherVars the otherVars to set
     */
    public void setOtherVars(ArrayList<Variable> otherVars) {
        this.otherVars = otherVars;
    }

    /**
     * @return the documentClassId
     */
    public int getDocumentClassId() {
        return constantParam_documentClassId;
    }

    /**
     * @param documentClassId the documentClassId to set
     */
    public void setDocumentClassId(int documentClassId) {
        this.constantParam_documentClassId = documentClassId;
    }

    /**
     * @return the constantParam_temporaryFolder
     */
    public String getConstantParam_temporaryFolder() {
        return constantParam_temporaryFolder;
    }

    /**
     * @param constantParam_temporaryFolder the constantParam_temporaryFolder to set
     */
    public void setConstantParam_temporaryFolder(String constantParam_temporaryFolder) {
        this.constantParam_temporaryFolder = constantParam_temporaryFolder;
    }

    /**
     * @return the constantParam_singleFromDBQuery
     */
    public String getConstantParam_singleFromDBQuery() {
        return constantParam_singleFromDBQuery;
    }

    /**
     * @param constantParam_singleFromDBQuery the constantParam_singleFromDBQuery to set
     */
    public void setConstantParam_singleFromDBQuery(String constantParam_singleFromDBQuery) {
        this.constantParam_singleFromDBQuery = constantParam_singleFromDBQuery;
    }
}
